<template>
  <div class="bigbox">
    <div class="topbox" :style="{height:isheight+'px'}">
        <el-form :model="form" ref="form"  label-position="left" label-width="80px" onsubmit="return false">
           <el-row :gutter="20">
            <el-col :span="5">
                <el-form-item label="用电企业">
                  <!-- <el-select v-model="form.orgNo" style="width: 100%;" size="small"  ref="qiye" @change="changeqi">
                    <el-option v-for="(item,index) in qiyelist" :key="index" :label="item.bizOrgName" :value="item.bizOrgNo"></el-option>
                  </el-select> -->
                  <FilterSelect2
                    v-model="form.orgNo"
                    :optionlist="qiyelist"
                    @updateModelValue="changeqi"
                    :valuekey="{ name: 'bizOrgName', value: 'bizOrgNo' }"
                  ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="配电室">
                  <!-- <el-select v-model="form.switchroomId" style="width: 100%;" size="small" @change="changepei">
                    <el-option v-for="(item,index) in peilist" :key="index" :label="item.name" :value="item.id"></el-option>
                  </el-select> -->
                  <FilterSelect2
                    v-model="form.switchroomId"
                    :optionlist="peilist"
                    @updateModelValue="changepei"
                    :valuekey="{ name: 'name', value: 'id' }"
                  ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="屏柜">
                  <!-- <el-select v-model="form.screenId" style="width: 100%;" size="small" @change="changeping">
                    <el-option v-for="(item,index) in pinglist" :key="index" :label="item.name" :value="item.id"></el-option>
                  </el-select> -->
                  <FilterSelect2
                    v-model="form.screenId"
                    :optionlist="pinglist"
                    @updateModelValue="changeping"
                    :valuekey="{ name: 'name', value: 'id' }"
                  ></FilterSelect2>
                </el-form-item>
              </el-col>
                <el-col :span="5">
                <el-form-item label="回路">
                  <!-- <el-select v-model="form.bayId" style="width: 100%;" size="small" >
                    <el-option v-for="(item,index) in huilist" :key="index" :label="item.name" :value="item.id"></el-option>
                  </el-select> -->
                  <FilterSelect2
                    v-model="form.bayId"
                    :optionlist="huilist"
                    :valuekey="{ name: 'name', value: 'id' }"
                  ></FilterSelect2>
                </el-form-item>
              </el-col>
              <template v-if="isshow">
                <el-col :span="4">
                <el-form-item label="是否计量">
                  <el-select v-model="form.isMeasure" style="width: 100%;" size="small" >
                    <el-option label="是" value="true"></el-option>
                    <el-option label="否" value="false"></el-option>
                    <el-option label="全部" value=""></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
                <el-col :span="8">
                <el-form-item label="日期">
                  <el-date-picker
                      v-model="date"
                      size="small"
                      type="datetimerange"
                      range-separator=""
                      start-placeholder="开始日期"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      end-placeholder="结束日期">
                    </el-date-picker>
                </el-form-item>
              </el-col>
              </template>
              <el-col :span="isshow?16:4" style="text-align: right !important;">
                  <el-button  @click="chongzhi"  size="small">重 置</el-button>
                  <el-button type="primary" @click="submit"  size="small">查 询</el-button>
                  <el-button type="text" v-if="isshow"  @click="zedie" size="small">收起 <i class="el-icon-arrow-up"></i> </el-button>
                  <el-button type="text" v-else @click="zedie" >展开 <i class="el-icon-arrow-down"></i> </el-button>
            </el-col>
           </el-row>
        </el-form>
     </div>
     <div class="bodybox" :style="{height:isshow?'calc(100% - 150px)':'calc(100% - 90px)'}">
      <div style="text-align: right;">
            <div class="title" style="display: inline-block;">{{ bizOrgName }}配电室{{ dates[0] }}~{{ dates[1] }} 用电量统计(kWh)</div>
            <div style="display: inline-block;width:30%;" class="tabbar">
              <el-button type="primary" size="small"  @click="daochu" ><i class="el-icon-upload2" >导出</i></el-button>
              <el-tooltip class="item" effect="dark" content="刷新" placement="top">
                <span style=" cursor: pointer;margin: 0 10px;" @click="shuaxin"><i class="el-icon-refresh-right"></i></span>
              </el-tooltip>
              <el-dropdown trigger="click" @command="command">
              <span class="el-dropdown-link">
                <el-tooltip class="item" effect="dark" content="密度" placement="top">
                <span style=" cursor: pointer;"><i class="el-icon-d-caret"></i></span>
              </el-tooltip>
              </span>
              <el-dropdown-menu slot="dropdown" >
                <el-dropdown-item command="默认">默认</el-dropdown-item>
                <el-dropdown-item command="中等">中等</el-dropdown-item>
                <el-dropdown-item command="紧凑">紧凑</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            </div>
          </div>
          <div style="height: 92%;margin-top: 10px;">
            <template>
              <el-table
              v-loading="loading"
              :data="tableData"
              style="width: 100%"
              border
              id="exportTabyong"
              ref="table"
              height="calc(100% - 1px)"
              :size="iissize"
              :header-cell-style="{'text-align':'center',background:'#f5f5f5'}"
                >
                <el-table-column
                  prop="devName"
                  label="设备名称"
                  width="400">
                </el-table-column>
                <el-table-column
                  prop="epowerStart"
                  label="初始电表示数"
                  align="right" >
                </el-table-column>
                <el-table-column
                  prop="epowerEnd"
                  label="终止电表示数"
                  align="right">
                </el-table-column>
                <el-table-column
                  prop="ydl"
                  label="电量"
                  align="right">
                </el-table-column>
              </el-table>
            </template>
          </div>
     </div>
  </div>
</template>

<script>
import { saveAs } from 'file-saver'
import { getceshijiekou, getpdaxialaapi, getpgxialaapi, gethuixialaapi } from '@/api/ElectricalDistribution/dayreport'
import {getyongdianlistapi,exportlistapi} from '@/api/ElectricalDistribution/electricity'
export default {
  name: 'ReportFormsIndex',

  data() {
    return {
      dates:[],
      id:'',
      val: '',
      bizOrgName:'',
      date:[],
      isheight: 80,
      isshow: false,
      qiyelist: [],
      peilist: [],
      pinglist: [],
      huilist: [],
      tableData: [],
      loading:false,
      iissize: 'medium',
      form: { switchroomId: '', isMeasure: '', bayId: '', screenId: '' }
    };
  },

  async mounted() {
    const date = new Date()
    const year = date.getFullYear() // 年
    const month = date.getMonth() + 1 // 月
    const day = date.getDate()

    this.date.push(year + '-' + month + '-' + '01' + ' 00:00:00')
    this.date.push(year + '-' + month + '-' + day + ' 23:59:59')
    this.dates.push(year + '-' + month + '-' + '01' + ' 00:00:00')
    this.dates.push(year + '-' + month + '-' + day + ' 23:59:59')
    // this.loading=true
    const res = await getceshijiekou()
      this.qiyelist = res.data.list
      this.form.orgNo =this.$store.state.statistics.userbizOrgNo ? this.$store.state.statistics.userbizOrgNo : res.data.list[0].bizOrgNo
      this.bizOrgName = this.qiyelist.filter(ele => ele.bizOrgNo === this.form.orgNo)[0]?.bizOrgName
      const ress = await getpdaxialaapi({ orgNo:this.$store.state.statistics.userbizOrgNo ? this.$store.state.statistics.userbizOrgNo : this.qiyelist[0].bizOrgNo })
      this.peilist = ress.data.list
      this.form.switchroomId = ress.data.list[0].id
      const resss = await getpgxialaapi(ress.data.list[0].id)
      this.pinglist = resss.data.list
      const ressss = await gethuixialaapi({ screenId: resss.data.list[0].id })
    this.huilist = ressss.data.list
    // if (!this.form.switchroomId) return this.$message.warning('配电室不可为空')
    // const table = await getyongdianlistapi({ ...this.form, startTime: this.date[0], endTime: this.date[1] })
    // this.tableData = table.data.list
    // this.loading=false
    this.submit()
  },

  methods: {
    zedie () {
      this.isshow = !this.isshow
      if (this.isshow) {
        this.isheight = 140
      } else {
        this.isheight = 80
      }
    },
    command(val) {
      if (val === '默认') {
        this.iissize = 'medium'
      } else if (val === '中等') {
        this.iissize = 'small'
      } else {
        this.iissize = 'mini'
      }
    },
    async changeqi(val) {
      this.$store.commit('setuserobject', val)
      this.val=val
      const ress = await getpdaxialaapi({ orgNo: val })
      this.peilist = ress.data.list
      this.form.switchroomId = ress.data.list[0] ? ress.data.list[0].id : ''
      if (ress.data.list[0]) {
        const resss = await getpgxialaapi(ress.data.list[0].id)
        this.pinglist = resss.data.list
        this.form.screenId = ''
        if (resss.data.list[0]) {
          const ressss = await gethuixialaapi({ screenId: resss.data.list[0].id })
          this.huilist = ressss.data.list
        } else {
          this.huilist = []
          this.form.bayId = ''
        }
      } else {
        this.pinglist = []
        this.huilist = []
      }
    },
    async changepei(val) {
      this.id=val
      const resss = await getpgxialaapi(val)
      this.pinglist = resss.data.list
      this.form.screenId = ''
      if (resss.data.list[0]) {
        const ressss = await gethuixialaapi({ screenId: resss.data.list[0].id })
        this.huilist = ressss.data.list
      } else {
        this.huilist = []
        this.form.bayId = ''
      }
    },
    async changeping(val) {
      const ressss = await gethuixialaapi({ screenId: val })
      this.huilist = ressss.data.list
      this.form.bayId = ''
    },
    async submit() {
      if (!this.form.switchroomId) return this.$message.warning('配电室不可为空')
      this.loading = true
      this.bizOrgName = this.qiyelist.filter(ele => ele.bizOrgNo === this.form.orgNo)[0]?.bizOrgName
      this.dates=this.date
      const table = await getyongdianlistapi({ ...this.form, startTime: this.date[0], endTime: this.date[1] })
      this.tableData = table.data.list
      this.loading = false

    },
    async chongzhi() {
      this.bizOrgName = this.qiyelist[0].bizOrgName
      this.form.screenId = ''
      this.form.bayId = ''
      this.form.isMeasure = ''
      this.loading = true
      const date = new Date()
    const year = date.getFullYear() // 年
    const month = date.getMonth() + 1 // 月
    const day = date.getDate()
      this.dates = ['2023-9-2 00:00:00', year + '-' + month + '-' + day + ' 23:59:59']
      this.date=['2023-9-2 00:00:00',year + '-' + month + '-' + day + ' 23:59:59']
    this.loading=false
    const res = await getceshijiekou()
      this.qiyelist = res.data.list
      this.form.orgNo = res.data.list[0].bizOrgNo
      const ress = await getpdaxialaapi({ orgNo: this.qiyelist[0].bizOrgNo })
      this.peilist = ress.data.list
      this.form.switchroomId = ress.data.list[0].id
      const resss = await getpgxialaapi(ress.data.list[0].id)
      this.pinglist = resss.data.list
      const ressss = await gethuixialaapi({ screenId: resss.data.list[0].id })
      this.huilist = ressss.data.list
      console.log(this.form);
      const table = await getyongdianlistapi({ ...this.form, startTime: this.date[0], endTime: this.date[1] })
    this.tableData = table.data.list
    
    },
    shuaxin() {
      this.submit()
      this.$message.success('数据更新成功')
    },
    async daochu() {
      const switchroomIds=this.peilist.filter(ele=>ele.id===this.form.switchroomId)[0].name
     const res= await exportlistapi({...this.form,startTime: this.date[0], endTime: this.date[1],orgName:this.bizOrgName,switchroomName:switchroomIds, isParams: true})
      console.log(res);
      const blobContent = new Blob([res])
      let url = window.URL.createObjectURL(blobContent)
      saveAs(url, '月用电量报表.xls')
    }
  },
};
</script>

<style lang="scss" scoped>
.bewdao{
  width: 63px;
  height: 32px;
}
.tabbar{
  text-align: right;
}
.title{
  text-align: center;
  margin-bottom: 10px;
  font-size: 20px;
}

.bigbox{
  background-color: #f1f1f1;
  padding: 10px;
  height: 100%;
}
.topbox{
  background-color: #fff;
  padding: 20px;
}
.bodybox{
  background-color: #fff;
  margin-top: 10px;
  padding: 20px;
}
</style>